<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="shortcut icon" href="../../../favicon.ico" type="image/x-icon">
	<title>controlled</title>
</head>
<body>
  <div id="test"></div>

  <script type="text/javascript" src="../../../js/react.development.js" ></script>
  <script type="text/javascript" src="../../../js/react-dom.development.js"></script>
  <script type="text/javascript" src="../../../js/babel.min.js"></script>
  <script type="text/javascript" src="../../../js/prop-types.js"></script>

  <script type="text/babel">
    // 受控组件:页面中所有输入类的dom,随着输入将值维护到状态中,需要用的时候直接从状态中取出的,即是受控组件
    // 优先使用受控组件,因为没有用ref,因为官方警告不要过度使用ref.即尽量不用ref
    class Login extends React.Component {
      state={
        username:'',
        password:''
      }

      onSubmit=(event)=>{
        event.preventDefault(); // 阻止表单提交
        const {username, password} = this.state;
        console.log('username:'+username+' password:'+password)
      }

      saveUsername=(event)=>{
        this.setState({
          username: event.target.value
        })
      }

      savePassword=(event)=>{
        this.setState({
          password: event.target.value
        })
      }

      render(){
        return (
          <form action="http://www.atguigu.com" onSubmit={this.onSubmit}>
            username: <input onChange={this.saveUsername} type="text" name="username" />
            password: <input onChange={this.savePassword} type="password" name="password" />
            <button>login</button>
          </form>
        )
      }
    }

    ReactDOM.render(<Login />, document.getElementById('test'));
  </script>
</body>